package com.molyfun.parents.activity.store;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseMainTabFragment;
import com.molyfun.parents.adapter.MainAskPagerAdapter;
import com.molyfun.parents.impl.TabFragmentRightMenuInterface;
import com.molyfun.parents.rest.bean.ExpenseRecordCountResponse;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.ThreeTabBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainTabStoreFragment extends BaseMainTabFragment implements ThreeTabBar.ThreeBarClickListener, TabFragmentRightMenuInterface, ViewPager.OnPageChangeListener,
        View.OnClickListener{

    @InjectView(R.id.choose_bar)
    ThreeTabBar mChooseBar;
    @InjectView(R.id.topic_viewpager)
    ViewPager mFeedViewPager;
    @InjectView(R.id.expense_record)
    TextView mExpenseRecord;

    MainAskPagerAdapter mMainAskPagerAdapter;

    List<Fragment> storeFragmentList;
    StoreFragment mUsingTopicFragment;
    StoreFragment mCompletedTopicFragment;
    StoreFragment mOverTopicFragment;

    public static MainTabStoreFragment newInstance() {
        return new MainTabStoreFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(!PreferencesStorageUtil.isUserEnter(getContext())){
            return super.onCreateView(inflater, container, savedInstanceState);
        }

        View view = inflater.inflate(R.layout.fragment_store, container, false);
        ButterKnife.inject(this, view);

        mChooseBar.setThreeBarClickListener(this);
        mExpenseRecord.setOnClickListener(this);

        storeFragmentList = new ArrayList<>();
        mUsingTopicFragment = StoreFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_ONE);
        storeFragmentList.add(mUsingTopicFragment);
        mCompletedTopicFragment = StoreFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_TWO);
        storeFragmentList.add(mCompletedTopicFragment);
        mOverTopicFragment = StoreFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_THREE);
        storeFragmentList.add(mOverTopicFragment);

        mMainAskPagerAdapter = new MainAskPagerAdapter(getChildFragmentManager(), storeFragmentList);
        mFeedViewPager.setAdapter(mMainAskPagerAdapter);
        mFeedViewPager.addOnPageChangeListener(this);
        mFeedViewPager.setCurrentItem(0);

//        getConsumeNum();
        return view;
    }

    @Override
    public void onParentClickTab() {

        if(PreferencesStorageUtil.isUserEnter(getContext()) && getView() != null){
            switch (mChooseBar.getCurrentChooseTab()){
                case ThreeTabBar.ThreeTabChooseTab.BAR_ONE:
                    if(mUsingTopicFragment != null){
                        mUsingTopicFragment.onParentTabClick();
                    }
                    break;
                case ThreeTabBar.ThreeTabChooseTab.BAR_TWO:
                    if(mCompletedTopicFragment != null){
                        mCompletedTopicFragment.onParentTabClick();
                    }
                    break;
                case ThreeTabBar.ThreeTabChooseTab.BAR_THREE:
                    if(mOverTopicFragment != null){
                        mOverTopicFragment.onParentTabClick();
                    }
                    break;
            }
        }

    }

    private void getConsumeNum(){
        Call<ExpenseRecordCountResponse> call = MFPApplication.getInstance().getRestClient().orderProductConsumeRecordNum();
        call.enqueue(new Callback<ExpenseRecordCountResponse>() {
            @Override
            public void onResponse(Call<ExpenseRecordCountResponse> call, Response<ExpenseRecordCountResponse> response) {
                if(response.body()!= null && response.body().getData() > 0){
                    mExpenseRecord.setText("消费记录(" + response.body().getData() + ")" );
                }
            }

            @Override
            public void onFailure(Call<ExpenseRecordCountResponse> call, Throwable t) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if(PreferencesStorageUtil.isUserEnter(getContext())){
            getConsumeNum();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.expense_record:
                //消费记录
                ExpenseRecordActivity.startActivity(getContext());
                break;
        }
    }

    @Override
    public int getRightBtnResource() {
        return R.mipmap.ic_tab_right_menu_add;
    }

    @Override
    public void onRightBtnClick() {}

    //nothing
    @Override
    public void onCancelEdit() {}

    @Override
    public void onThreeBarClick(int chooseTab) {
        switch (chooseTab) {
            case ThreeTabBar.ThreeTabChooseTab.BAR_ONE:
                mFeedViewPager.setCurrentItem(0);
                break;
            case ThreeTabBar.ThreeTabChooseTab.BAR_TWO:
                mFeedViewPager.setCurrentItem(1);
                break;
            case ThreeTabBar.ThreeTabChooseTab.BAR_THREE:
                mFeedViewPager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position == 0){
            mChooseBar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_ONE);
        }else if(position == 1){
            mChooseBar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_TWO);
        }else{
            mChooseBar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_THREE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {}

    @Override
    public void onAddHomeland() {}

    @Override
    public void onAddProduct() {
//        ProductListActivity.startActivity(getContext());
    }

}
